package com.qzdsoft.erpcloud.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.common.domain.query.sys.QueryParams;
import com.qzdsoft.erpcloud.domain.sys.SysMenuButton;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.SysMenuButtonService;
import com.qzdsoft.erpcloud.vo.sys.SysMenubuttonQueryInfo;
import com.qzdsoft.erpcloud.vo.sys.ZtreeNode;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.sys.SysMenuButtonStatusEnum;
import com.qzdsoft.vo.sys.SysMenuButtonTypeEnum;

/**
 * Created by Administrator on 2017/6/20 0020.
 * 菜单管理
 * http://localhost/sys/menuManage/index
 */
@Controller
@RequestMapping("/sys/menuManage")
public class SysMenuButtonController {
	private static final String BASEPATH = "sys/menubutton";
    private static final Logger logger = LoggerFactory.getLogger(SysMenuButtonController.class);
    @Autowired
    private SysMenuButtonService menuService;
    @Autowired
    private SysMenuButtonService sysMenuButtonService;
   /* @Autowired
    private SessionService sessionService;*/

    @Permission(validate=false)
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(HttpServletRequest request, Model model,QueryParams params) {
//        List<EnumPo> typeList = new ArrayList<EnumPo>();
//        Map<Object, Object> typeMap = new HashMap<Object, Object>();
//        for(SysMenuButtonTypeEnum e : SysMenuButtonTypeEnum.values()){
//            SysUtils.enums(typeList, typeMap, e.getKey(), e.getVal());
//        }
//        List<EnumPo> statusList = new ArrayList<EnumPo>();
//        Map<Object, Object> statusMap = new HashMap<Object, Object>();
//        for(SysMenuButtonStatusEnum e : SysMenuButtonStatusEnum.values()){
//            SysUtils.enums(statusList, statusMap, e.getKey(), e.getVal());
//        }
//        model.addAttribute("typeList", typeList);
//        model.addAttribute("typeMap", typeMap);
//
//        model.addAttribute("statusList", statusList);
//        model.addAttribute("statusMap", statusMap);
//        //所有的上级菜单，url为空，type=1
//        model.addAttribute("catalogMenuList",menuService.getAllTopMenu());
////        return "sys/menu_button";
        return BASEPATH+"/index";
    }
    /**
     * 页面数据初始化查询
     * @param info
     * @return
     */
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse<SysMenuButton> data(SysMenubuttonQueryInfo info) {
        if(info.isFirst()) {
        	 return LayTableResponse.nullDataPage();
        }else {
        	return menuService.findAllData(info);
        }
    }
//    public Object data(HttpServletRequest request, SysMenubuttonQueryParam param) {
//        Integer count = menuService.selectDataCount(param.getName(),param.getUrl());
//        List list = menuService.selectData(param.getName(),param.getUrl(), param.getiDisplayStart(), param.getiDisplayLength());
//        return new DataTablePo(count, list, param.getsEcho());
//    }
    @Permission(validate=false)
    @RequestMapping("/addinit")
    public ModelAndView toadd(ModelAndView mv) {
    	logger.debug("菜单管理新增");
    	mv.setViewName(BASEPATH+"/add");
    	return mv;
    }
    /**
     * 上级菜单数据查询
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/findAllMenu")
    public Response<ZtreeNode> findAllMenu() {
    	Integer type = 0;//菜单
    	return new Response(ResultEnum.SUCCESS,sysMenuButtonService.selectAllMenu(type));
    }
    /**
     * 新增保存
     */
    @SysLog(description="菜单管理新增保存")
    @ResponseBody
    @RequestMapping("/add")
    public Object add(SysMenuButton sysMenuButton) {
    	logger.debug("菜单新增:id={},super_id={}",sysMenuButton.getId(),sysMenuButton.getSuper_id());
//       return Response.success("");
    	return menuService.edit(sysMenuButton);
    }
    /**
     * 修改页面跳转
     * @param request
     * @param id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(ModelAndView model, @RequestParam("id") Integer id) {
    	modelAndView(model);
        SysMenuButton sysMenuButton=menuService.selectById(id);
        model.addObject("item", sysMenuButton);
        model.setViewName(BASEPATH+"/edit");
        return model;
    }
    /**
     * 修改保存
     * @param sysMenuButton
     * @return
     */
//    @Permission(validate=false)
    @SysLog(description="菜单管理修改保存")
    @ResponseBody
    @RequestMapping("/edit_execute")
    public Object editExecute(SysMenuButton sysMenuButton) {
    	logger.debug("菜单修改:id={},super_id={}",sysMenuButton.getId(),sysMenuButton.getSuper_id());
//       return Response.success("");
    	return menuService.edit(sysMenuButton);
    }
   
    /**
     * 菜单详情
     */
    @Permission(validate=false)
    @RequestMapping("/view")
    public ModelAndView showDetail(ModelAndView model, @RequestParam("id") Integer id) {
        SysMenuButton sysMenuButton=menuService.selectById(id);
        model.addObject("item", sysMenuButton);
        model.setViewName(BASEPATH+"/view");
        return model;
    }
    /**
     * 删除
     * @param request
     * @param id
     * @return
     */
    @SysLog(description="菜单管理删除")
    @ResponseBody
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return menuService.del(id);
    }
    
    public static void modelAndView(ModelAndView model) {
    	 List<EnumPo> typeList = new ArrayList<EnumPo>();
         Map<Object, Object> typeMap = new HashMap<Object, Object>();
         for(SysMenuButtonTypeEnum e : SysMenuButtonTypeEnum.values()){
             SysUtils.enums(typeList, typeMap, e.getKey(), e.getVal());
         }
         List<EnumPo> statusList = new ArrayList<EnumPo>();
         Map<Object, Object> statusMap = new HashMap<Object, Object>();
         for(SysMenuButtonStatusEnum e : SysMenuButtonStatusEnum.values()){
             SysUtils.enums(statusList, statusMap, e.getKey(), e.getVal());
         }
         model.addObject("typeList", typeList);
         model.addObject("typeMap", typeMap);

         model.addObject("statusList", statusList);
         model.addObject("statusMap", statusMap);
    }
}
